package com.qf.dao.Impl;

import com.qf.dao.AddressDao;
import com.qf.domain.Address;
import com.qf.utils.DataSourceUtil;
import com.qf.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> findAddListByUid(int uid) {
        QueryRunner qf = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select id , detail, name , phone, uid, level from tb_address where uid=?";
        try {
            List<Address> addList = qf.query(sql, new BeanListHandler<>(Address.class), uid);
            return addList;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询地址列表失败!!");
        }
        return null;
    }

    @Override
    public void insert(Address address) {
        String sql = "INSERT INTO `tb_address` (`id`, `detail`, `name`, `phone`, `uid`, `level`) VALUES (null, ?, ?, ?, ?, ?)";
        QueryRunner qf = new QueryRunner(DataSourceUtil.getDataSource());
        try {
            qf.update(sql , address.getDetail(), address.getName(), address.getPhone(), address.getUid(), address.getLevel());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败!!");
        }
    }

    @Override
    public void delectById(String id) {
        String sql = "delete from tb_address where id = ?";
        QueryRunner qf = new QueryRunner(DataSourceUtil.getDataSource());
        try {
            qf.update(sql , id);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败!!");
        }
    }

    @Override
    public void update(Address address) {
        String sql = "UPDATE `tb_address` SET `detail`=?, `name`=?, `phone`=?, `uid`=?, `level`=? WHERE (`id`=?)";
        QueryRunner qf = new QueryRunner(DataSourceUtil.getDataSource());
        try {
            qf.update(sql , address.getDetail(), address.getName(), address.getPhone(),address.getUid(),address.getLevel(), address.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!");
        }
    }

    @Override
    public void clearDefaultAddress(int uid) {
        Connection conn = TxUtils.getConn();
        String sql = " UPDATE `tb_address` SET `level`= 0 WHERE (`uid`=?) ";
        QueryRunner qf = new QueryRunner();
        try {
            qf.update(conn,sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!!");
        }
    }

    @Override
    public void setDefalutAddress(String aid) {
        Connection conn = TxUtils.getConn();
        String sql = "UPDATE `tb_address` SET `level`= 1 WHERE (`id`=?)";
        QueryRunner qf = new QueryRunner();
        try {
            qf.update(conn,sql,aid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!!!");
        }
    }
}
